local BuffPanel = require "View/Expedition/SubPanel/ExpeditionBuffPanel"
---@class UIExpeditionSelectBuff : UIBase
UIExpeditionSelectBuff = UIBase.New(PanelResNames.UIExpeditionSelectBuff)
local t = UIExpeditionSelectBuff

function t.Init(stageId,buffId,callback,callbackTB)
    t.stageId = stageId
    t.buffId= buffId
    t.callback = callback
    t.callbackTB = callbackTB
    UIBase.Init(t)
end

function t:OnCreate()
    self.ctrlBuff = self:GetController("ctrlBuff")
    self.btnSure = self:GetChild("ConfirmButton")
    ---@type ExpeditionBuffPanel[]
    self.buffItemList = {nil,nil,nil}
    for k = 1,3 do
        local panel = self:CreateComponent(BuffPanel,self:GetChild("Card"..k))
        table.insert(self.buffItemList,panel)
    end

    self:RegisterOnClick(self.btnSure,self.ClickSure)
    self:RegisterEvent(Config.EventType.ExpeditionUseBuffSuc,self.UseBuffSucMsg)
end

function t:OnEnable()
    self.ctrlBuff.selectedIndex = 0
    self:Refresh()
end
function t:OnDisable()
end
function t:OnDestroy()
end

function t:Refresh()
    local index = nil
    if t.stageId == 0 then
        index = 1
    else
        index = t.stageId
    end
    local num = logicMgr.ExpeditionMgr.GetCurStageCfgParam(index,"f_Buff_1")
    if num ~= 0 then
        self.buffItemList[1]:RefreshHotSprings(index)
        self.buffItemList[2]:RefreshAltar(index)
        self.buffItemList[3]:RefreshBuff(self.buffId[1])
    else
        self.buffItemList[1]:RefreshBuff(self.buffId[1])
        self.buffItemList[2]:RefreshBuff(self.buffId[2])
        self.buffItemList[3]:RefreshBuff(self.buffId[3])
    end
end
function t:ClickClose()
    closeUI(PanelResNames.UIExpeditionSelectBuff)
end
function t:ClickSure()
    local index = self.ctrlBuff.selectedIndex
    if index == 0 then return end
    ExpeditionHandler.ReqUseBuff(t.stageId,index)
    logicMgr.ExpeditionMgr.SetSelectAwardIndex(index)
end
function t:UseBuffSucMsg()
    self:ClickClose()
    if t.callback and t.callbackTB then
        t.callback(t.callbackTB)
    end
    t.callback = nil
    t.callbackTB = nil
    t.buffList = nil
end
return t